<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/visit.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%!
	// 查询详细
	public Message queryVisitDetail(Integer surveyId, Integer visitId) {
		Message msg = new Message();
		List<Survey> surveys = QBuilder.db("basedb").queryHql("from Survey where id = " + surveyId);
		if (isEmpty(surveys))
			return new Message(30041, "没有这份量表");
		Survey survey = surveys.get(0);
		String title = survey.getTitle();
		Map surveyMap = new HashMap();
		int id = survey.getId();
		surveyMap.put("title", title);
		surveyMap.put("id", id);
		
		boolean all = true;
		// 查询副标题
		List<SurveyQuestion> questList = QBuilder.db("basedb").queryHql("from SurveyQuestion where status = " + STATUS_ENABLE + " and surveyId = "+ id + " order by showOrder asc");
		Iterator<SurveyQuestion> it = questList.iterator();
		List<SurveyQuestion> titles = new ArrayList<SurveyQuestion>();
		List<SurveyQuestion> mulInputs = new ArrayList<SurveyQuestion>();
		String questIds = "-1";
		while (it.hasNext()) {
			SurveyQuestion sq = it.next();
				// 问题
			questIds += ("," + sq.getId());
		}
		// 根据问题id查出问题选项
		Map<Integer, List<Map<String, Object>>> questIdMap = new HashMap<Integer, List<Map<String, Object>>>(); // 问题和问题选项集合
		List<SurveyOptions> optionList = QBuilder.db("basedb").queryHql("from SurveyOptions where status = " + STATUS_ENABLE + " and questId in(" + questIds + ") order by showOrder asc");
		// 查出病人的答案
		List<SurveyAnswer> answerList = QBuilder.db("basedb").queryHql("from SurveyAnswer where status = " + STATUS_ENABLE + " and questId in(" + questIds + ") and visitId = " + visitId + " and surveyId = " + surveyId);
		for (int i = 0; i < optionList.size(); i++) {
			SurveyOptions option = optionList.get(i);
			String content = option.getContent();
			Integer optionId = option.getId();
			Integer questId = option.getQuestId();
			List<Map<String, Object>> options = questIdMap.get(questId);
			if (null == options) {
				options = new ArrayList<Map<String, Object>>();
			}
			Map<String, Object> anOption = new HashMap<String, Object>();
			anOption.put("content", content);
			anOption.put("score", option.getScore());
			anOption.put("option_id", optionId);
			Iterator<SurveyAnswer> itor = answerList.iterator();
			while (itor.hasNext()) {
				SurveyAnswer sa = itor.next();
				int type = sa.getType();
				Integer quest_id = sa.getQuestId();
				if (questId != null && questId.equals(quest_id)) {
					if (type == SURVEY_QUESTION_TYPE_INPUT_V2) { // 填空题
						anOption.put("answer", sa.getContent());
						itor.remove();
					} else if (type == SURVEY_QUESTION_TYPE_SINGLE_V2 || type == SURVEY_QUESTION_TYPE_MULTI_V2 || type == SURVEY_QUESTION_TYPE_SINGLE_NOMUST_V2|| type == SURVEY_QUESTION_TYPE_GRADE_V2) {
						Integer option_id = Tools.getInt(sa.getContent(), -99999);
						if (option_id.equals(optionId)) {
							anOption.put("answer", "1");
							itor.remove();
						}
					}
				}
			}
			if (all) {
				options.add(anOption);
				questIdMap.put(questId, options);
			} else {
				if (anOption.get("answer") != null) {
					options.add(anOption);
					questIdMap.put(questId, options);
				}
			}
		}
		// 问题选项加到对应的问题中
		LinkedList<Map<String, Object>> quests = new LinkedList<Map<String, Object>>(); // 问题列表
		for (int i = 0; i < questList.size(); i++) {
			SurveyQuestion quest = questList.get(i);
			String questTitle = quest.getTitle();
			Integer questId = quest.getId();
			Integer type = quest.getType();
			Integer parentId = quest.getParentId();
			Integer necessary = quest.getNecessary();
			Map<String, Object> aQuest = new HashMap<String, Object>();
			aQuest.put("title", questTitle);
			aQuest.put("quest_id", questId);
			aQuest.put("type", type);
			if (type == SURVEY_QUESTION_TYPE_SINGLE_NOMUST_V2 || type == SURVEY_QUESTION_TYPE_SINGLE_V2 || type == SURVEY_QUESTION_TYPE_MULTI_V2 || type == SURVEY_QUESTION_TYPE_GRADE_V2) {
				List<Map<String, Object>> tempList = questIdMap.get(questId);
				for (int j = 0; tempList != null && j < tempList.size(); j++) {
					Map<String, Object> tempMap = tempList.get(j);
					if (tempMap.get("answer") == null) {
						tempMap.put("answer", "0");
					}
				}
			}
			if (type == SURVEY_QUESTION_TYPE_INPUT_V2) {
				Iterator<SurveyAnswer> itor = answerList.iterator();
				while (itor.hasNext()) {
					SurveyAnswer sa = itor.next();
					Integer quest_id = sa.getQuestId();
					if (questId != null && questId.equals(quest_id)) {
						if (sa.getType() == SURVEY_QUESTION_TYPE_INPUT_V2) { // 填空题
							aQuest.put("answer", sa.getContent());
							itor.remove();
						} 
					}
				}
			}
			aQuest.put("options", questIdMap.get(questId));
			aQuest.put("parent_id", parentId);
			aQuest.put("necessary", necessary);
			aQuest.put("show_order", quest.getShowOrder());
			quests.add(aQuest);
		}
		// 把多重填空子题目放入大标题中
		LinkedList<Map<String, Object>> dest = new LinkedList<Map<String, Object>>();
		dest.addAll(quests);
		// 根据showorder排序
		quests.clear();
		quests.addAll(dest);
		Collections.sort(quests, new Comparator<Map<String, Object>>() {

			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				return Tools.getInt(o1.get("show_order") + "", 0).compareTo(Tools.getInt(o2.get("show_order")+"", 0));
			}
		});

		// 问题加入大分类
		dest.clear();
		dest.addAll(quests);
		for (int i = 0; i < titles.size(); i++) {
			SurveyQuestion sq = titles.get(i);
			String questTitle = sq.getTitle();
			Integer questId = sq.getId();
			Integer type = sq.getType();
			Integer parentId = sq.getParentId();
			Integer necessary = sq.getNecessary();
			Map<String, Object> aQuest = new HashMap<String, Object>();
			aQuest.put("title", questTitle);
			aQuest.put("quest_id", questId);
			aQuest.put("type", type);
			aQuest.put("parent_id", parentId);
			aQuest.put("necessary", necessary);
			aQuest.put("show_order", sq.getShowOrder());
			dest.addFirst(aQuest);
			Iterator<Map<String, Object>> itor = quests.iterator();
			while (itor.hasNext()) {
				Map<String, Object> map = itor.next();
				Integer sqId = (Integer)map.get("parent_id");
				if (questId.equals(sqId)) {
					List<Map<String, Object>> childList = (List<Map<String, Object>>)aQuest.get("quests");
					if (childList == null) {
						childList = new ArrayList<Map<String, Object>>();
						aQuest.put("quests", childList);
					}
					childList.add(map);
					itor.remove();
					dest.remove(map);
				}
			}
		}
		Collections.sort(dest,new Comparator<Map>(){
		  public int compare(Map map1,Map map2){// 定义逆序排序规则 从小到大排序
			if ( Tools.getInt(map1.get("show_order") + "", 0).compareTo(Tools.getInt(map2.get("show_order") + "", 0)) > 0) {
			  return 1;
			}
			if (Tools.getInt(map1.get("show_order") + "", 0).equals(Tools.getInt(map2.get("show_order") + "", 0))) {
			  return 0;
			}
			return -1;
		  }
		});
		surveyMap.put("quests", dest);
		msg.put("data", surveyMap);
		return msg;
	}
%>
<%
	Integer surveyId = input.getInt("survey_id", 173);
	Integer visitId = input.getInt("visit_id", 3212);
	Tools.encryptPrint(out, queryVisitDetail(surveyId, visitId));
%>